import os
from google.appengine.ext.webapp import template
import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

from data.datamodel import Store
import settings

class AddStoreHandler(webapp.RequestHandler):
    def post(self):
        store = Store(name=self.request.get('storeName'))
        store.put()
        self.redirect(settings.STORE_ROOT)

    def get(self):
        template_values = {

        }

        path = settings.TEMPLATE_DIR + "store/Add.html"
        self.response.out.write(template.render(path, template_values))

class EditStoreHandler(webapp.RequestHandler):
    def lookupEntity(self):
        storeKey = db.Key.from_path('Store',int(self.request.get('storeKey')))
        store = db.get(storeKey)
        return store
    def post(self):
        store = self.lookupEntity()
        store.name = self.request.get('storeName')
        store.put()
#        self.redirect(settings.STORE_ROOT)
    def get(self):
        store = self.lookupEntity()
        template_values = {
            'store': store,
        }

        path = settings.TEMPLATE_DIR + "store/Edit.html"
        self.response.out.write(template.render(path, template_values))


class ManageStoresHandler(webapp.RequestHandler):
    def get(self):

        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'


        template_values = {
            'basePath': '/store',
            'url': url,
            'url_linktext': url_linktext,
            'loadingHtml' : 'Loading...',
        }
        path = settings.TEMPLATE_DIR + "Manage.html"
        self.response.out.write(template.render(path, template_values))

class GetStoresHandler(webapp.RequestHandler):
    def get(self):

        storeKeyStr = self.request.get('storeKey')

        if storeKeyStr:
            templateName = "Read.html"
            storeKey = db.Key.from_path('Store',int(storeKeyStr))
            store = db.get(storeKey)
            template_values = {
                'store': store,
            }
        else:
            templateName = "List.html"
            stores_query = Store.all().order('name')
            stores = stores_query.fetch(10)
            template_values = {
                'stores': stores,
            }


        path = settings.TEMPLATE_DIR + "store/" + templateName
        self.response.out.write(template.render(path, template_values))


application = webapp.WSGIApplication(
                                     [(settings.STORE_ROOT, ManageStoresHandler),
                                      (settings.STORE_ROOT + '/list', GetStoresHandler),
                                      (settings.STORE_ROOT + '/edit', EditStoreHandler),
                                      (settings.STORE_ROOT + '/add', AddStoreHandler)],
                                     debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
